package com.fulin.leetcode;

/**
 * @Description 盛水最多的容器
 * @Author fulin16
 * @Date 2020/12/9 10:54
 **/
public class Num11 {

    //双指针算法
    public static int maxArea(int[] height) {
        if(height == null || height.length <= 1) return 0;
        int right = height.length - 1;
        int left = 0;

        int result = 0;
        while(left < right){
            int leftHigh = height[left];    //左板高度
            int rightHigh = height[right];  //右板高度
            int area = (right - left) * (Math.min(leftHigh,rightHigh));
            result = Math.max(result,area);
            int i = leftHigh < rightHigh ? left ++ : right --;  //决定移动左板还是移动右板(谁的高度小就移动谁)
        }
        return result;
    }

    //双指针增强算法,跳过了无效的矮板
    public static int maxAreaProcess(int[] height) {
        if(height == null || height.length <= 1) return 0;
        int right = height.length - 1;
        int left = 0;

        int result = 0;
        while(left < right){
            int leftHigh = height[left];
            int rightHigh = height[right];
            int area = (right - left) * (Math.min(leftHigh,rightHigh));
            result = Math.max(result,area);
//            int i = leftHigh < rightHigh ? left ++ : right --;
            if(leftHigh < rightHigh){
                //左板矮,移动左板,并不是所有的左板都有资格当选
                int nowLeft = leftHigh;
                while(left < right && nowLeft >= height[left] ){
                    ++left;
                }
            }else{
                //右板矮,移动右板,并不是所有的右板都有资格当选
                int nowRight = rightHigh;
                while(right > left && nowRight >= height[right] ){
                    --right;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] height = {4,3,2,1,4};
        System.out.println(maxArea(height));
    }


}
